
import { Component, OnInit } from '@angular/core';
import { SysRole, SysRoleService } from 'src/app/service/sys-role.service';
import { TableStyle, TableStyleService } from 'src/app/service/table-style.service';
import { MessageService } from 'primeng/api';
import { NzNotificationService } from 'ng-zorro-antd';
import { BreadFieldService } from 'src/app/service/bread-field.service';
import { JsonResult } from 'src/app/model/JsonResult';
import { UUID } from 'angular2-uuid';
import { forkJoin, Subject } from 'rxjs';
import { debounce, debounceTime } from 'rxjs/operators';
import { AuthService } from 'src/app/service/auth.service';

@Component({
  selector: 'app-sys-role',
  templateUrl: './sys-role.component.html',
  styleUrls: ['./sys-role.component.css']
})
export class SysRoleComponent implements OnInit {
  ngOnInit(): void {}

  vari:{// 变量集合 - 属性集合
    'mouseOver_rowIndex':number,// 鼠标悬停的行号
    'emptyRowCount':number, // 空白行数
    'statistics':{ [key:string]:{ label:string,value:number } }, // 有统计的字段的集合
    'reqTip':string,
    'currentRole':SysRole
  } = { mouseOver_rowIndex:-1,'emptyRowCount':1,'statistics':{},'reqTip':'请求失败，请检查网络连接！','currentRole':null};

  
  mouseOverUUID:string= '';

  constructor(
    private pMsgService:MessageService,
    private noti:NzNotificationService,
    private tableStyleService:TableStyleService,
    public detailService:SysRoleService,
    public breadField:BreadFieldService,
    public authService:AuthService,
    
  ) { 
    this.init();
    this.initPerm();
  }

  //#region 初始化指定角色的权限
  initPerm(){
    this.authService.refresh(this.vari.currentRole).subscribe(
      ( jr:JsonResult ) =>{
        if( jr.code !==1 ){
          this.msg(jr.msg);
          return
        }

        this.msg('请求权限数据成功！');
      },error => this.msg('请求权限数据时出现异常！')
    )
  }

  //#region 初始化表格数据、样式数据、表格编辑状态下的下拉数据
  init(){
    // 查询获取表格编辑状态下的下拉数据
    this.detailService.initOpts().subscribe(
      ( res:JsonResult ) =>{
        if( res.code !==1 ){
          this.msg('初始化下拉数据时出现异常！');
          return
        }
        
      },error => this.msg('初始化下拉数据时出现异常！')
    )

    this.refresh();// 查询获取表格数据、样式数据


  }
  //#endregion

  //#region 刷新表格数据
  refresh(){
    this.detailService.refresh().subscribe(
      ( res:JsonResult ) =>{
        if( res.code !==1 ){
          this.msg(res.msg);
          return
        }

        // 默认选中第一个角色
        this.setCurrentRole(this.detailService.tableDataDisplay[0]);
        this.msg('刷新完毕！');
      },error => this.msg('刷新数据时出现异常！')
    )
  }
  //#endregion

  //#region 鼠标移动相关的事件
  mouseOver(uuid:string){
  }

  mouseLeave(rowIndex:number){
  }
  //#endregion

  //#region 新增一个空行
  addEmptyRow(){
    this.detailService.addEmptyRow();
  }
  //#endregion

  //#region 开启编辑状态
  startEdit(rowIndex:number,uuid:string,event){
    event.preventDefault();
    const res:string = this.detailService.startEdit(uuid);
    if( res.length>0 )
      this.msg(res);
  }
  //#endregion

  //#region 取消编辑
  cancelEdit(rowIndex:number,uuid:string){
    this.detailService.editCache[uuid].edit = false;
  }
  //#endregion

  //#region 删除行，兼容纯前端行、前后端行
  deleteRow(rowIndex:number,uuid:string){
    this.detailService.deleteRow(rowIndex,uuid).subscribe(
      ( res:JsonResult ) =>{
        if( res.code !==1 ){
          this.msg(res.msg);
          return
        }

        this.msg('删除成功！');
      },error => this.msg('删除数据时出现异常！')
    )
  }
  //#endregion

  cancelDeleteRow(){
    // console.log('取消了删除行');
  }

  //#region 指定字段 input 变动触发其他业务逻辑
  inputChange(dataRow:SysRole,tsRow:TableStyle){
    
    if( tsRow.field_name === 'amount' ){
      // 指定字段的 input 变动触发其他业务逻辑
    }

    if( tsRow.field_name === 'credit_amount' ){
      // 指定字段的 input 变动触发其他业务逻辑
    }

  }
  //#endregion

  //#region 变动触发
  onChange_category(e){
    // 变动触发
  }

  onChange_enum(e:string,fieldName:string){
    // 变动触发
  }
  //#endregion

  //#region 过滤
  filter(opts:string[],fieldName:string){
    this.detailService.filter(opts,fieldName);
  }
  //#endregion

  //#region 自定义彩色消息通知
  msg(content:string) {
    this.pMsgService.add({key: 'custom', severity:'info', summary: '通知', detail:content});
  }
  //#endregion

  //#region 保存编辑行
  saveEdit(rowIndex:number,uuid:string){
    this.detailService.saveEdit(rowIndex,uuid).subscribe(
      ( res:JsonResult ) =>{
        if( res.code !==1 ){
          this.msg('保存时出现异常！');
          return
        }

        this.msg('保存完毕！');
      },error => this.msg('保存时出现异常！')
    )
  }
  //#endregion

  printTD(){
    //console.log(this.detailService.filterOpts);
  }

  //#region 恢复删除
  restoreDeleted(rowIndex:number,uuid:string){
    this.detailService.restoreDeleted(this.detailService.tableDataDisplay[rowIndex].iid,rowIndex).subscribe(
      ( res:JsonResult ) =>{
        if( res.code !==1 ){
          this.msg(res.msg);
          return
        }

        this.msg('恢复成功！');
      },error => this.msg('恢复被删除数据时出现异常！')
    )
  }
  //#endregion


  printd(){

  }


  //#region 开启权限表编辑状态
  startEditPerm(rowIndex:number,uuid:string,e){
    event.preventDefault();
    this.authService.permEditCache[uuid].edit = true;
  }
  //#endregion

  //#region 设置当前选中的角色
  setCurrentRole(curRole:SysRole){
    this.vari.currentRole = {...curRole};
    console.log('业务组件中选中的role:' + curRole.iid + ',' + curRole.roleid);
    this.initPerm();
  }
  //#endregion

  //#region 保存编辑
  onClickSaveEdit(uuid:string,rowIndex:number){
    this.authService.saveEdit(uuid,rowIndex).subscribe(
      ( jr:JsonResult ) =>{
        if( jr.code !==1 ){
          this.msg(jr.msg);
          return
        }

        this.msg('保存成功！');
      },error => this.msg('保存时出现异常！')
    )
  }
  //#endregion

  //#region 取消编辑
  onClickCancelEdit(uuid:string,rowIndex:number){
    this.authService.cancelEdit(uuid,rowIndex);
  }
  //#endregion
}
    